package com.java.simple.study.jvm.memstruct.directmemory;

import com.java.simple.study.tool.Sleeper;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoujunlin
 * @date 2022年08月09日 22:26
 * @desc -XX:MaxDirectMemorySize=1g
 */
public class BufferTest2 {

    private static final int _20MB = 1024 * 1024 * 20;

    public static void main(String[] args) {
        List<ByteBuffer> byteBufferList = new ArrayList<>();
        int count = 0;
        try {
            while (true) {
                ByteBuffer byteBuffer = ByteBuffer.allocateDirect(_20MB);
                byteBufferList.add(byteBuffer);
                count++;
                Sleeper.sleep(0.1);
            }
        } finally {
            //java.lang.OutOfMemoryError: Direct buffer memory
            // 未设置直接内存大小 count=180
            // 设置直接内存大小1g后 count=51
            System.out.println("执行次数" + count);
        }
    }

}
